<?php
require("functions.php");
require("dbconfig.php");
require("./class/sqlHelper.php");
require("./class/pageHelper.php");
$db = new Opmysqli(HOST, USER, PASSWORD, DBNAME);
?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>电商主页搜索</title>	
<meta name="viewport" content="width=device-width,initial-scale=1.0">
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet">
<style type="text/css">
body
{
	font-family:微软雅黑;
	font-size:16px;
	color:#036;
  background:#F8F8F8;
	}
.con_from
{
    margin-top:40px;
}
.color_c
{
    color:red;
    margin-top:30px;
}
img
{
  width:200px;
  height:200px;
}
.font_con
{
  color:#0063DC;
}
.cart_con{
	margin-left:20px;
	margin-top:20px;
}
</style>
<!--[if lt IE 9]>
  <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
  <?php
    require ("./help/myCartHtml.php");
  ?>
    <div class="con_from">
        <center>
            <form class="form-search" action="homePage.php" method="get">
              <div class="input-append">
              <input type="text" class="span6 search-query" value="<?php if(!empty($_GET['keywords'])){
                                        echo $_GET['keywords']; }?>" name="keywords">
                <button type="submit" class="btn btn-primary">搜索</button>
              </div>
            </form>
        </center>
    </div> 

<?php
/*定义一个封装搜索条件的数组变量
$whereList = array();
if (!empty($_GET['keywords']))
{
    $whereList[] = "name like '%{$_GET['keywords']}%'";
}
/*if (!empty($_GET['title']))
{
    $whereList = "name like '%{$_GET['keywords']}%'";
    print_r($whereList);
}
if (!empty($_GET['author']))
{
    $whereList = "name like '%{$_GET['keywords']}%'";
    print_r($whereList);
}
 *
 *
//组装搜索条件
$where = '';
if (count($whereList) > 0)
{
    $where = " where ".implode(" and ", $whereList); 
}
$sql = "select * from clothes {$where} order by addtime desc";
echo $sql."<br />";
echo "<br />".$where;*/
if($resError = $db->msg_error())
{
  echo "<div class=\"well color_c\">";
    echo $resError;
    echo "</div>";
    exit(0);
}
//收到用户的关键字进行查询
if(!empty($_GET['keywords']))
{
    $searchQuery = "select id, name, price, pic from clothes";
    $userSearch = trim($_GET['keywords']);
    //调用函数过滤数据进行模糊查询
    $query = filterSearchWords($searchQuery, $userSearch);
   
    //clothes表中结果集的行数
    $totalNums = $db->getRowsNum($query);
    //每页显示9条数据
    $nums = 9;
    $page = new Page($totalNums, $nums);
    $sql = $query." ".$page->limit;
    $searchTotal = $db->getRowsArray($sql);
    ?>
    <ul class="thumbnails">
    <?php
    foreach ($searchTotal as $total)
    {
      ?>
        <li class="span4">
            <div class="thumbnail">
              <a href="detailInfo.php?id=<?php echo $total['id'];?>" class="thumbnail"><img src="./uploads/<?php echo $total['pic'];?>" alt="300x200"></a>
              <div class="caption">
                <p class="font_con">
                  <?php 
                    $nameCon = $total['name'];
                    echo str_replace($_GET['keywords'], "<span style=\"color:red;\">{$_GET['keywords']}</span>", $nameCon);
                  ?>
                </p>
                <p>&yen;&nbsp;&nbsp;<span style="color:red;"><?php echo $total['price'];?></span></p>
              </div>
            </div>
        </li>
<?php
    }
?>
    </ul>
    <div class="pagination">
      <?php 
        echo $page->fpage(array(0, 3));
        echo "<br />";
      ?>
    <ul class="pager">
      <?php
      echo $page->fpage(array(4, 5, 6, 7, 8));
      ?>
      &nbsp;&nbsp;
      <div class="input-append">
      <?php echo $page->fpage(array(9)); ?>
     </div>
    </ul>
  </div>
<?php
  }
  else
  {
    ?>
    <div class="row">
      <div class="span6 offset3">
        <img src="./media/snlogo.png" style="width:100px; height:40px;"><br /><br />
        <img src="./media/honghaizi.jpg" style="width:450px; height:400px;">
      </div>
    </div>
    <?php
  }
?>

<?php
$db->close_conn();
?>
</div>
<script src="./bootstrap/js/bootstrap.min.js"></script>
<script src="./js/jquery-1.9.1.min.js"></script>
<script src="./js/checkVali.js"></script>
</body>
</html>
    




